<?php
class code_baseMod extends commonMod {
	public function __construct() {
		parent::__construct ();
	}
	
	public function common_list($tables, $as_name = NULL, $return_file, $join, $where, $order, $limit = null) {
		$sql = $this->sql_str ( $tables, $as_name = NULL, $return_file, $join, $where, $order, $limit = null );
		$comment = $this->model->query ( $sql );
		if ($comment) {
			foreach ( $comment as $key => $val ) {
				$reply_where = "A.reply_id = " . $val ['id'];
				$reply_sql = $this->sql_str ( $tables, $as_name = NULL, $return_file, $join, $reply_where, $order );
				$reply = $this->model->query ( $reply_sql );
				$comment [$key] ['reply'] = $reply;
				$good = $this->model->table ( 'good_comment' )->where ( "info_id = " . $val ['id'] )->count ();
				$comment [$key] ['good'] = $good;
				if ($reply) {
					$comment [$key] ['reply_num'] = count ( $reply );
				} else {
					$comment [$key] ['reply_num'] = 0;
				}
				unset ( $reply );
			}
		}
		return ( array ) $comment;
	}
	
	public function sql_str($tables, $as_name = NULL, $return_file, $join, $where, $order, $limit = null) {
		if (! is_array ( $as_name )) {
			$as_name = array (
					"A",
					"B",
					"C",
					"D",
					"E",
					"F",
					"G",
					"H",
					"I" 
			);
		}
		$sql = "SELECT " . $return_file;
		if (is_array ( $tables ) && is_array ( $join ) && is_array ( $as_name )) {
			foreach ( $tables as $k => $v ) {
				if ($k == '0') {
					$sql .= " FROM {$this->model->pre}" . $v . " " . $as_name [$k];
				} else {
					$sql .= " LEFT JOIN {$this->model->pre}" . $v . " " . $as_name [$k] . " ON " . $join [$k - 1];
				}
			}
			$sql .= " WHERE {$where} ORDER BY {$order}";
			if ($limit) {
				$sql .= " LIMIT {$limit}";
			}
		}
		return $sql;
	}
}